home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / dec92.zip / 1012038A < prev    next >
Text File  |  1992-10-13  |  3KB  |  126 lines

  1. //-------------------------------------------------
  2. // Person.m - implementation for the Person object.
  3. //-------------------------------------------------
  4.  
  5. #import <stdio.h>
  6. #import <stdlib.h>
  7. #import <strings.h>
  8. #import "Person.h"
  9.  
  10. @implementation Person
  11.  
  12. //-------------------------------------------------
  13. // Instance methods.
  14. //-------------------------------------------------
  15. - initWithName:(const char*)aName
  16.   age:(int)anAge
  17. //
  18. // Initialize newly created instance.
  19. //
  20. {
  21. [super init];
  22. [[self setName:aName] setAge:anAge];
  23. return self;
  24. }
  25.  
  26. //-------------------------------------------------
  27. - free
  28. //
  29. // Tidy up the instance. Free malloced storage.
  30. //
  31. {
  32. if( name )
  33.     free( name );
  34.  
  35. return [super free];
  36. }
  37.  
  38. //-------------------------------------------------
  39. // Set instance variable values.
  40. //-------------------------------------------------
  41. - setName:(const char*)aName
  42. {
  43. int    len = strlen(aName);
  44.  
  45. if( name )
  46.     free( name );
  47.     
  48. name = (char*)malloc( len + 1 );
  49. strcpy( name, aName );
  50.  
  51. return self;
  52. }
  53.  
  54. //-------------------------------------------------
  55. - setAge:(int)anAge
  56. {
  57. age = anAge;
  58. return self;
  59. }
  60.  
  61. //-------------------------------------------------
  62. // Get instance variable values.
  63. //-------------------------------------------------
  64. - (const char*)name { return (const char*)name; }
  65.  
  66. //-------------------------------------------------
  67. - (int)age { return age; }
  68.  
  69. //-------------------------------------------------
  70. // General instance methods.
  71. //-------------------------------------------------
  72. - printInfo
  73. //
  74. // Print info for this instance of Person.
  75. //
  76. {
  77. printf( "%s info: name %s, age %d \n",
  78.     [[self class] name], name, age );
  79.  
  80. return self;
  81. }
  82.  
  83. //-------------------------------------------------
  84. // Archiving methods.
  85. //-------------------------------------------------
  86. - read:(NXTypedStream*)stream
  87. //
  88. // Unarchive this instance from a typed stream.
  89. //
  90. {
  91. int    namelen;
  92.  
  93. // Read our superclass.
  94. [super read:stream];
  95.  
  96. // Read our instance variables.
  97. NXReadTypes( stream, "ii", &age, &namelen );
  98. name = malloc( namelen + 1 );
  99. NXReadArray( stream, "c", namelen, name );
  100.  
  101. return self;
  102. }
  103.  
  104. //-------------------------------------------------
  105. - write:(NXTypedStream*)stream
  106. //
  107. // Archive this instance to a typed stream.
  108. //
  109. {
  110. int    namelen;
  111.  
  112. // Write our superclass.
  113. [super write:stream];
  114.  
  115. // Write our instance variables.
  116. namelen = strlen(name);
  117. NXWriteTypes( stream, "ii", &age, &namelen );
  118. NXWriteArray( stream, "c", namelen, name );
  119.  
  120. return self;
  121. }
  122.  
  123. //-------------------------------------------------
  124. @end
  125.  
  126.